package qing.controller.cms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import qing.entity.Navi;
import qing.entity.Navigation;
import qing.service.NaviService;

@Controller
@RequestMapping("/cms/navigation")
public class NavigationController {

	@Autowired
	private NaviService idao;
	
	@RequestMapping("list")
	public String showlists(Map<String, Object> map) {
		map.put("navigationlist", idao.list());
		return "navigationlist";
	}
	@RequestMapping(value="api/list",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String apilist() {
		 Map<String,Object> map=new HashMap<String,Object>();
		 map.put("status", 1);
		 map.put("msg","");
		 List<Navi> list1=new ArrayList<>();
		 String hql = "select n from Navigation n where n.classlayer=:layer and pid=:pid and islock=0 order by taxis,id";
		 List<Navigation> query =idao.createQuery(hql).setParameter("layer", 1).setParameter("pid", 0).list();
		 for (Navigation navi : query) {
			 List<Navi> list2=new ArrayList<>();
			 List<Navigation> query2 =idao.createQuery(hql).setParameter("layer", 2).setParameter("pid", navi.getId()).list();
			
			 for (Navigation navi2 : query2) {
				 //得到三级
				 List<Navigation> query3 =idao.createQuery(hql).setParameter("layer", 3).setParameter("pid", navi2.getId()).list();
				  List<Navi> list3=new ArrayList<>();				 
				 for (Navigation navi3 : query3) {
					 list3.add(new Navi(navi3.getTitle(), navi3.getSubtitle(), navi3.getLinkurl(), navi3.getCssstyle(),null));
				 }
				 list2.add(new Navi(navi2.getTitle(), navi2.getSubtitle(), navi2.getLinkurl(), navi2.getCssstyle(),list3));
			 }
			 list1.add(new Navi(navi.getTitle(), navi.getSubtitle(), navi.getLinkurl(), navi.getCssstyle(),list2));
			 
		}
		 map.put("list", list1); 
		 return JSON.toJSONString(map);
	}
	
	//绑定会员模型
	@ModelAttribute
	public void getnavigation(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			System.out.println("modeAttribute");
			map.put("navigation", idao.get(id));
		}
	}
	
//添加用户显示
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addget(Map<String, Object> map) {
		map.put("navigation",new Navigation());
		return "addnavigation";
	}
 //提交新增
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String addpost(@Valid Navigation u, BindingResult result, Map<String, Object> map) {		
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}
			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("navigation",u);
			return "addnavigation";
		}
		idao.save(u);
		return "redirect:list";
	}
//修改初始化
	@RequestMapping(value = "add/{id}", method = RequestMethod.GET)
	public String editinit(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("navigation", idao.get(id));
		return "addnavigation";
	}  
//提交修改
	@RequestMapping(value = "add", method = RequestMethod.PUT)
	public String eidtpost(@Valid Navigation u, BindingResult result, Map<String, Object> map) {
		System.out.println("开始修改，并重定向……" + u.toString());
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}

			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("navigation",u);
			return "addnavigation";
		}
				
		idao.update(u);
		return "redirect:list";
	}
//删除用户
	@RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		System.out.println("dele:" + id);
		idao.deleteById(id);
		return "redirect:../list";
	}


	
	

}
